package com.freshcity.component.guide
{
	import com.freshcity.graphics.GuideWin;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.pblabs.debug.Logger;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class GuideTips extends Sprite
	{
		private var _tips:GuideWin;
		private var _txt:TextField;
		
		private var _callBack:Function;
		private var _message:String;
		private var _msgNum:int;
		private var _timeOut:int;
		private var _callBackPara:*;
		private var layerManager:LayerManager;
		private var _icon:MovieClip;
		private var _isCenter:Boolean;
		
		public function GuideTips(layerManager:LayerManager)
		{
			this.tabEnabled = false;
			this.mouseEnabled = false;
			this.layerManager = layerManager;
			
			this._tips = new GuideWin();
			_tips.width = 450;
			addChild(this._tips);
			
			this._txt = _tips.content;
			this._icon = _tips.mouseIcon;
		}
		
		public function updateTxt(arg1:String):void
		{
			this.visible = true;
			this._message = arg1;
			if(!this.parent){
				LayerManager.guideLayer.addChild(this);
				this.x = (stage.stageWidth - _tips.width) / 2;
				this.y = stage.stageHeight;
				var ypos:Number;
				if(_isCenter){
					ypos = (stage.stageHeight - _tips.height)/2;	
				}else{
					ypos = (stage.stageHeight - _tips.height)-30;	
				}
				TweenMax.to(this,0.3,{autoAlpha:1,y:ypos,onComplete:showTxt});
			}else{
				showTxt();
			}
		}
		
		private function showTxt():void
		{
			this._msgNum = StringUtil.getANSILength(_message);
			onTick(1);
			//TweenLite.to(_mask,2,{onComplete:activeMouseEvent,x:_txt.width});
		}
		
		private function onTick(count:int):void
		{
			if(count <= _msgNum){
				var tempText:String = this._message.substr(0, count);
				tempText="".concat("<p style= 'font-weight:bold'>", tempText, "</p>");
				this._txt.htmlText = tempText;
				count ++;
				TweenLite.delayedCall(1,onTick,[count],true);
			}else{
				activeCallBack();
			}
		}
		
		public function updateCallBack(arg1:Function,timeout:int,para:Array,isCenter:Boolean):void
		{
			this._callBack = arg1;
			this._callBackPara = para;
			this._timeOut = timeout;
			this._isCenter = isCenter;
		}
		
		private function activeCallBack():void
		{
			if (_callBack == null) 
			{
				this.mouseEnabled=false;
				LayerManager.guideLayer.stage.removeEventListener(flash.events.MouseEvent.CLICK, this.onMouseClick);
			}
			else if(_timeOut>0)
			{
				TweenLite.delayedCall(_timeOut,_callBack,_callBackPara);
			}
			else{
				this.mouseEnabled=true;
				this.buttonMode=true;
				this._icon.visible=true;
				LayerManager.guideLayer.stage.addEventListener(flash.events.MouseEvent.CLICK, this.onMouseClick);
			}
		}		
		
		private function onMouseClick(arg1:MouseEvent):void
		{
			if (this._callBack != null) 
			{
				this._icon.visible=false;
				_callBack.apply(null,_callBackPara);
			}
		}
		
		public function clear():void
		{
			this.visible = false;
			this._message = "";
			this._isCenter = false;
			this._msgNum = 0;
			this._timeOut = 0;
			this._icon.visible = false;
			this.mouseChildren = false;
			this.mouseEnabled = false;
			this._callBack = null;
			LayerManager.guideLayer.stage.removeEventListener(MouseEvent.CLICK, this.onMouseClick);
		}
		
		public function hide():void
		{
			TweenMax.to(this,0.3,{autoAlpha:0,y:stage.stageHeight,onComplete:destroy});
		}
		
		public function destroy():void
		{
			clear();
			if(this.parent)this.parent.removeChild(this);
		}
	}
}